<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: orders_status.php 85 2013-09-21 12:03:27Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 85 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2013-09-21 12:03:27 +0000 (Sat, 21 Sep 2013) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

require('includes/application_top.php');

switch ($_GET['action']) {
	case 'insert':
	case 'save':
		$orders_status_id = $_GET['oID'];
		
		$languages = get_languages();
		for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
			$orders_status_name_array = $_POST['orders_status_name'];
			$language_id = $languages[$i]['id'];
			
			$sql_data_array = array('orders_status_name' => $orders_status_name_array[$language_id]);
			
			if ($_GET['action'] == 'insert') {
				if (!not_null($orders_status_id)) {
			
					$next_id = $db->db_query("	SELECT 
													MAX(orders_status_id) AS orders_status_id 
												FROM 
													".TABLE_ORDERS_STATUS."");
			
					$orders_status_id = $next_id->fields['orders_status_id'] + 1;
				}
			
				$insert_sql_data = array(	'orders_status_id' => $orders_status_id,
											'language_id' => $language_id);
			
				$sql_data_array = array_merge($sql_data_array, $insert_sql_data);
				$db->db_perform(TABLE_ORDERS_STATUS, $sql_data_array);
			
			} elseif($_GET['action'] == 'save') {
				$db->db_perform(TABLE_ORDERS_STATUS, $sql_data_array, 'update', "orders_status_id = ".$db->db_prepare($orders_status_id)." AND language_id = '".$language_id."'");
			}
		}
		
		if ($_POST['default'] == 'on')
			$db->db_query("UPDATE ".TABLE_CONFIGURATION." SET configuration_value = ".$db->db_prepare($orders_status_id)." WHERE configuration_key = 'DEFAULT_ORDERS_STATUS_ID'");
		
		
		redirect(href_link(FILENAME_ORDERS_STATUS, 'oID='.$orders_status_id.(isset($_GET['page']) ? '&page='.$_GET['page']:'')));
	break;

	case 'deleteconfirm':
		$oID = $_GET['oID'];
		
		$orders_status = $db->db_query("SELECT 
											configuration_value 
										FROM 
											".TABLE_CONFIGURATION." 
										WHERE 
											configuration_key = 'DEFAULT_ORDERS_STATUS_ID'");
		
		if ($orders_status->fields['configuration_value'] == $oID){ 
			$db->db_query("	UPDATE 
								".TABLE_CONFIGURATION." 
							SET 
								configuration_value = '' 
							WHERE 
								configuration_key = 'DEFAULT_ORDERS_STATUS_ID'");
		}
		
		
		$db->db_query("DELETE FROM ".TABLE_ORDERS_STATUS." WHERE orders_status_id = ".$db->db_prepare($oID));
		
		redirect(href_link(FILENAME_ORDERS_STATUS, (isset($_GET['page']) ? 'page='.$_GET['page'] : '')));
	break;

	case 'delete':
		$oID = $_GET['oID'];
		
		$status = $db->db_query("SELECT COUNT(*) AS count FROM ".TABLE_ORDERS." WHERE orders_status = ".$db->db_prepare($oID));
		
		$remove_status = true;
		
		if ($oID == DEFAULT_ORDERS_STATUS_ID) {
			$remove_status = false;
			$message_stack->add(ERROR_REMOVE_DEFAULT_ORDER_STATUS, 'error');
		} elseif($status->fields['count'] > 0) {
			$remove_status = false;
			$message_stack->add(ERROR_STATUS_USED_IN_ORDERS, 'error');
		} else {
			$history = $db->db_query("SELECT COUNT(*) AS count FROM ".TABLE_ORDERS_STATUS_HISTORY." WHERE orders_status_id = ".$db->db_prepare($oID));
			if ($history->fields['count'] > 0) {
				$remove_status = false;
				$message_stack->add(ERROR_STATUS_USED_IN_HISTORY, 'error');
			}
		}
	break;
}
require(DIR_WS_INCLUDES.'metatag.php');
?>
</head>
<body>
<?php require(DIR_WS_INCLUDES.'header.php'); ?>
<div id="wrapper">
<table class="outerTable" cellspacing="0" cellpadding="0">
  <tr>
    <td class="columnLeft2" width="<?php echo BOX_WIDTH; ?>" valign="top">
		<?php require(DIR_WS_INCLUDES.'column_left.php'); ?>
	</td>
    <td class="boxCenter" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="2">
      <tr>
        <td>
			<table class="table_pageHeading" border="0" width="100%" cellspacing="0" cellpadding="0">
		    	<tr>
			     <td class="pageHeading">
			        <?php echo HEADING_TITLE; ?>
			     </td>
			   </tr>
			</table>
		</td>
      </tr>
      <tr>
        <td valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0" class="dataTable">
              <tr class="dataTableHeadingRow">
                <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_ORDERS_STATUS; ?></td>
				<td class="dataTableHeadingContent" align="center">ID</td>
                <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_ACTION; ?>&nbsp;</td>
              </tr>
			<?php
			  $sql = "SELECT orders_status_id, orders_status_name from ".TABLE_ORDERS_STATUS." WHERE language_id = '".$_SESSION['languages_id']."' ORDER BY orders_status_id";
			  $query = page_break_create($_POST['per_site'], $_GET['page'], $sql, 'orders_status.php');
			  $orders_status = $query['query'];
			  
			  $rows=1;
			  while (!$orders_status->EOF) {
			    if (((!$_GET['oID']) || ($_GET['oID'] == $orders_status->fields['orders_status_id'])) && (!$oInfo) && (substr($_GET['action'], 0, 3) != 'new')) {
			      $oInfo = new objectInfo($orders_status->fields);
			    }

			    if ( (is_object($oInfo)) && ($orders_status->fields['orders_status_id'] == $oInfo->orders_status_id) ) {
			      echo '<tr class="dataTableRowSelected" onmouseover="this.style.cursor=\'pointer\'" onclick="document.location.href=\''.href_link(FILENAME_ORDERS_STATUS, 'oID='.$oInfo->orders_status_id.'&action=edit'.(isset($_GET['page']) ? '&page='.$_GET['page'] : '')).'\'">'."\n";
			    } else {
				  	if ($rows % 2 == 0)
			    		$f = 'dataTableRow';
			    	else
			    		$f = '';
			      echo '<tr class="'. $f.'" onmouseover="this.className=\'dataTableRowOver\';this.style.cursor=\'pointer\'" onmouseout="this.className=\''. $f.'\'" onclick="document.location.href=\''.href_link(FILENAME_ORDERS_STATUS, 'oID='.$orders_status->fields['orders_status_id'].(isset($_GET['page']) ? 'page='.$_GET['page'] : '')).'\'">'."\n";
			    }

			    if (DEFAULT_ORDERS_STATUS_ID == $orders_status->fields['orders_status_id']) {
			      echo '<td class="dataTableContent"><b>'.$orders_status->fields['orders_status_name'].' ('.TEXT_DEFAULT.')</b></td>'."\n";
			    } else {
			      echo '<td class="dataTableContent">'.$orders_status->fields['orders_status_name'].'</td>'."\n";
			    }
			?>
				<td align="center">
					<?php echo $orders_status->fields['orders_status_id'] ?>
				</td>
                <td class="dataTableContent" align="right">
				<?php if ( (is_object($oInfo)) && ($orders_status->fields['orders_status_id'] == $oInfo->orders_status_id) ) { echo image(DIR_WS_IMAGES.'icon_arrow_right.gif', ''); } else { echo '<a href="'.href_link(FILENAME_ORDERS_STATUS, 'oID='.$orders_status->fields['orders_status_id'].(isset($_GET['page']) ? '&page='.$_GET['page'] : '')).'">'.image(DIR_WS_IMAGES.'icon_info.gif', IMAGE_ICON_INFO).'</a>'; } ?>&nbsp;</td>
			<?php
				echo '</tr>';
			  	$rows++;
			  	$orders_status->MoveNext();
				}
			?>
            </table>
			<table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr class="page_break">
				<td class="smallText" valign="top" width="33.3%"><?php echo 'Angezeigt werden '.$query['from'].' bis '.$query['to'].' (von insgesamt '.$query['total'].' Bestellstatus)'; ?></td>
				<td class="smallText" align="center" width="33.3%"></td>
				<td class="smallText" align="right" width="33.3%">
					Bestellstatus pro Seite: <?php echo $query['page_break']->perSiteDropdown($_GET['page']); ?>
				</td>
			  </tr>
<?php
if (substr($_GET['action'], 0, 3) != 'new') {
?>
              <tr>
                <td colspan="2" align="right"><?php echo '<a class="button" onclick="this.blur();" href="'.href_link(FILENAME_ORDERS_STATUS, 'action=new'.(isset($_GET['page']) ? '&page='.$_GET['page'] : '')).'">'.BUTTON_INSERT.'</a>'; ?></td>
              </tr>
<?php
}
?>
            </table>
			</td>
<?php
  $heading = array();
  $contents = array();
  switch ($_GET['action']) {
    case 'new':
      $heading[] = array('text' => '<b>'.TEXT_INFO_HEADING_NEW_ORDERS_STATUS.'</b>');

      $contents = array('form' => draw_form('status', FILENAME_ORDERS_STATUS, 'action=insert'.(isset($_GET['page']) ? '&page='.$_GET['page'] : '')));
      $contents[] = array('text' => TEXT_INFO_INSERT_INTRO);

      $orders_status_inputs_string = '';
      $languages = get_languages();
      for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
        $orders_status_inputs_string .= '<br />'.image(DIR_WS_LANGUAGES.$languages[$i]['directory'].'/admin/images/'.$languages[$i]['image']).'&nbsp;'.draw_input_field('orders_status_name['.$languages[$i]['id'].']');
      }

      $contents[] = array('text' => '<br />'.TEXT_INFO_ORDERS_STATUS_NAME.$orders_status_inputs_string);
      $contents[] = array('text' => '<br />'.draw_checkbox_field('default').' '.TEXT_SET_DEFAULT);
      $contents[] = array('align' => 'center', 'text' => '<br /><input type="submit" class="button" onclick="this.blur();" value="'.BUTTON_INSERT.'"/> <a class="button" onClick="this.blur();" href="'.href_link(FILENAME_ORDERS_STATUS, (isset($_GET['page']) ? 'page='.$_GET['page'] : '')).'">'.BUTTON_CANCEL.'</a>');
      break;

    case 'edit':
      $heading[] = array('text' => '<b>'.TEXT_INFO_HEADING_EDIT_ORDERS_STATUS.'</b>');

      $contents = array('form' => draw_form('status', FILENAME_ORDERS_STATUS, 'oID='.$oInfo->orders_status_id.'&action=save'.(isset($_GET['page']) ? '&page='.$_GET['page']:'')));
      $contents[] = array('text' => TEXT_INFO_EDIT_INTRO);

      $orders_status_inputs_string = '';
      $languages = get_languages();
      for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
        $orders_status_inputs_string .= '<br />'.image(DIR_WS_LANGUAGES.$languages[$i]['directory'].'/admin/images/'.$languages[$i]['image']).'&nbsp;'.draw_input_field('orders_status_name['.$languages[$i]['id'].']', get_orders_status_name($oInfo->orders_status_id, $languages[$i]['id']));
      }

      $contents[] = array('text' => '<br />'.TEXT_INFO_ORDERS_STATUS_NAME.$orders_status_inputs_string);
      if (DEFAULT_ORDERS_STATUS_ID != $oInfo->orders_status_id) $contents[] = array('text' => '<br />'.draw_checkbox_field('default').' '.TEXT_SET_DEFAULT);
      $contents[] = array('align' => 'center', 'text' => '<br /><button type="submit" class="button" onclick="this.blur();">'. BUTTON_UPDATE.'</button> <a class="button" onClick="this.blur();" href="'.href_link(FILENAME_ORDERS_STATUS, 'oID='.$oInfo->orders_status_id.(isset($_GET['page']) ? '&page='.$_GET['page'] : '')).'">'.BUTTON_CANCEL.'</a>');
      break;

    case 'delete':
      $heading[] = array('text' => '<b>'.TEXT_INFO_HEADING_DELETE_ORDERS_STATUS.'</b>');

      $contents = array('form' => draw_form('status', FILENAME_ORDERS_STATUS, 'oID='.$oInfo->orders_status_id .'&action=deleteconfirm'.(isset($_GET['page']) ? '&page='.$_GET['page'] : '')));
      $contents[] = array('text' => TEXT_INFO_DELETE_INTRO);
      $contents[] = array('text' => '<br /><b>'.$oInfo->orders_status_name.'</b>');
      if ($remove_status) $contents[] = array('align' => 'center', 'text' => '<br /><input type="submit" class="button" onclick="this.blur();" value="'.BUTTON_DELETE.'"/> <a class="button" onclick="this.blur();" href="'.href_link(FILENAME_ORDERS_STATUS, 'oID='.$oInfo->orders_status_id.(isset($_GET['page']) ? '&page='.$_GET['page'] : '')).'">'.BUTTON_CANCEL.'</a>');
      break;

    default:
      if (is_object($oInfo)) {
        $heading[] = array('text' => '<b>'.$oInfo->orders_status_name.'</b>');

        $contents[] = array('align' => 'center', 'text' => '<a class="button" onclick="this.blur();" href="'.href_link(FILENAME_ORDERS_STATUS, 'oID='.$oInfo->orders_status_id.'&action=edit'.(isset($_GET['page']) ? '&page='.$_GET['page'] : '')).'">'.BUTTON_EDIT.'</a> <a class="button" onclick="this.blur();" href="'.href_link(FILENAME_ORDERS_STATUS, 'oID='.$oInfo->orders_status_id.'&action=delete'.(isset($_GET['page'])?'&page='.$_GET['page']:'')).'">'.BUTTON_DELETE.'</a>');

        $orders_status_inputs_string = '';
        $languages = get_languages();
        for ($i = 0, $n = sizeof($languages); $i < $n; $i++) {
          $orders_status_inputs_string .= '<br />'.image(DIR_WS_LANGUAGES.$languages[$i]['directory'].'/admin/images/'.$languages[$i]['image']).'&nbsp;'.get_orders_status_name($oInfo->orders_status_id, $languages[$i]['id']);
        }

        $contents[] = array('text' => $orders_status_inputs_string);
      }
      break;
  }

  if ( (not_null($heading)) && (not_null($contents)) ) {
    echo '            <td width="25%" class="border" valign="top">'."\n";
    $box = new box;
    echo $box->infoBox($heading, $contents);
    echo '            </td>'."\n";
  }
?>
          </tr>
        </table></td>
      </tr>
    </table></td>
  </tr>
</table>
</div>
<?php require(DIR_WS_INCLUDES.'footer.php'); ?>
</body>
</html>
<?php require(DIR_WS_INCLUDES.'application_bottom.php'); ?>